package com.qfedu.tantu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.tantu.config.RedisKeyConfig;
import com.qfedu.tantu.dao.ReserveDao;
import com.qfedu.tantu.dto.ReserveDto;
import com.qfedu.tantu.entity.Reserve;
import com.qfedu.tantu.service.intf.HousPayService;
import com.qfedu.tantu.util.AliPayUtil;
import com.qfedu.tantu.util.JedisUtil;
import com.qfedu.tantu.util.StringUtil;
import com.qfedu.tantu.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @Description:
 * @Author: 赵金帅
 * @Date: 2022/07/04/20:57
 */
@Service
public class HousPayServiceImpl implements HousPayService {
    @Resource
    ReserveDao dao;
    @Override
    public R create(ReserveDto dto) {
        if (dto!=null){
            if (dto.getToken()>0&& dto.getIdnum()!=null){
                QueryWrapper<Reserve> wrapper= new QueryWrapper<>();
                wrapper.eq("token",dto.getToken());
                wrapper.eq("idnum",dto.getIdnum());
                Reserve reserve = dao.selectOne(wrapper);
                if (reserve!=null){
                    //生成订单编号
                    String no=createNo(Integer.parseInt(dto.getIdnum()));
                    //支付
                    String res= AliPayUtil.createPay(no,dto.getHousprice(),dto.getHousname());
                    if(StringUtil.isNotEmpty(res)){
                        JedisUtil.addHash(RedisKeyConfig.HOUS_PAY_TOKEN+dto.getToken(),dto.getIdnum()+"",no);
                        return R.ok(res);
                    }
                }
            }
        }
        return R.fail("支付失败,请检查订单");
    }

    @Override
    public R query(int token,String idnum) {
        if (token>0&& idnum!=null){
            QueryWrapper<Reserve> wrapper= new QueryWrapper<>();
            wrapper.eq("token",token);
            wrapper.eq("idnum",idnum);
            Reserve reserve = dao.selectOne(wrapper);
            if (reserve!=null){
                String no = JedisUtil.getHash(RedisKeyConfig.HOUS_PAY_TOKEN + token, idnum+"");
                if (AliPayUtil.queryPay(no).equals("TRADE_SUCCESS")){
                    if (dao.updateFlag(2,token,idnum)>=0){
                        return R.ok();
                    }
                }
            }
        }
       return R.fail("未支付");
    }
    @Transactional //开启事务
    @Override
    public R refund(int token,String idnum) {
        if (token>=0&&idnum!=null){
            QueryWrapper<Reserve> wrapper= new QueryWrapper<>();
            wrapper.eq("token",token);
            wrapper.eq("idnum",idnum);
            Reserve reserve = dao.selectOne(wrapper);
            if (reserve!=null){
                String no = JedisUtil.getHash(RedisKeyConfig.HOUS_PAY_TOKEN + token, idnum+"");
                boolean isReturn = AliPayUtil.refound(no, 0.01);
                if (isReturn){
                    JedisUtil.delhashKeys(RedisKeyConfig.HOUS_PAY_TOKEN + token, idnum+"");
                    if (dao.updateFlag(1,token,idnum)>=0){
                        return R.ok();
                    }
                    return R.ok("网络错误");
                }
            }
        }
        return R.fail("退款失败");
    }
    //生成订单号，不超过64位
    private String createNo(int id){
        return "15038957423"+id;
    }
}
